package work

import (
	"parking-lot-go-admin/config"
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/work"
	"strings"

	"github.com/gin-gonic/gin"
)

func Insert(c *gin.Context) {

	type ImageStruct struct {
		Name string `json:"name"`
	}

	requestDataStruct := struct {
		CarNumber string        `json:"car_number"`
		ParkName  string        `json:"park_name"`
		Question  string        `json:"question"`
		Image     []ImageStruct `json:"images"`
	}{}

	c.ShouldBindJSON(&requestDataStruct)

	systemSection := config.Cfg.Section("SYSTEM")
	imgUrl := systemSection.Key("IMAGE_URL").String()

	imageStr := ""
	for _, v := range requestDataStruct.Image {
		v.Name = strings.Replace(v.Name, imgUrl, "", -1)
		imageStr += v.Name + ","
	}
	imageStr = strings.TrimRight(imageStr, ",")

	// //管理员id
	adminId, _ := c.Get("admin_id")

	var workOrderStruct work.WorkOrderStruct
	model.CreateModelData(
		model.Db,
		workOrderStruct.TableName(),
		map[string]interface{}{
			"admin_id":   adminId,
			"car_number": requestDataStruct.CarNumber,
			"park_name":  requestDataStruct.ParkName,
			"question":   requestDataStruct.Question,
			"image":      imageStr,
		},
	)

	controller.Success(c, "")
}
